/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.persistencia.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import mrp.beans.util.EntityManagerUtil;
import mrp.persistencia.controller.exceptions.NonexistentEntityException;
import mrp.persistencia.controller.exceptions.PreexistingEntityException;
import mrp.persistencia.controller.exceptions.RollbackFailureException;
import mrp.persistencia.entity.Inventario;
import mrp.persistencia.entity.Talla;
import mrp.persistencia.entity.Tacosuela;
import mrp.persistencia.entity.Tallatacosuela;

/**
 *
 * @author root
 */
public class TallatacosuelaJpaController implements Serializable {

    public TallatacosuelaJpaController() {
        
        this.emf = EntityManagerUtil.getEntityManagerFactory();
    }

    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }


    public void create(Tallatacosuela tallatacosuela) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            
            em = getEntityManager();
            em.getTransaction().begin();
            Inventario inventario = tallatacosuela.getInventario();
            if (inventario != null) {
                inventario = em.getReference(inventario.getClass(), inventario.getCodinventario());
                tallatacosuela.setInventario(inventario);
            }
            Talla tallaFk = tallatacosuela.getTallaFk();
            if (tallaFk != null) {
                tallaFk = em.getReference(tallaFk.getClass(), tallaFk.getCodtalla());
                tallatacosuela.setTallaFk(tallaFk);
            }
            Tacosuela tacosuelaFk = tallatacosuela.getTacosuelaFk();
            if (tacosuelaFk != null) {
                tacosuelaFk = em.getReference(tacosuelaFk.getClass(), tacosuelaFk.getCodmateriaprima());
                tallatacosuela.setTacosuelaFk(tacosuelaFk);
            }
            em.persist(tallatacosuela);
            if (inventario != null) {
                Tallatacosuela oldTallatacosuelaFkOfInventario = inventario.getTallatacosuelaFk();
                if (oldTallatacosuelaFkOfInventario != null) {
                    oldTallatacosuelaFkOfInventario.setInventario(null);
                    oldTallatacosuelaFkOfInventario = em.merge(oldTallatacosuelaFkOfInventario);
                }
                inventario.setTallatacosuelaFk(tallatacosuela);
                inventario = em.merge(inventario);
            }
            if (tallaFk != null) {
                tallaFk.getTallatacosuelaList().add(tallatacosuela);
                tallaFk = em.merge(tallaFk);
            }
            if (tacosuelaFk != null) {
                tacosuelaFk.getTallatacosuelaList().add(tallatacosuela);
                tacosuelaFk = em.merge(tacosuelaFk);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findTallatacosuela(tallatacosuela.getCodtallatacosuela()) != null) {
                throw new PreexistingEntityException("Tallatacosuela " + tallatacosuela + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tallatacosuela tallatacosuela) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            
            em = getEntityManager();
            em.getTransaction().begin();
            Tallatacosuela persistentTallatacosuela = em.find(Tallatacosuela.class, tallatacosuela.getCodtallatacosuela());
            Inventario inventarioOld = persistentTallatacosuela.getInventario();
            Inventario inventarioNew = tallatacosuela.getInventario();
            Talla tallaFkOld = persistentTallatacosuela.getTallaFk();
            Talla tallaFkNew = tallatacosuela.getTallaFk();
            Tacosuela tacosuelaFkOld = persistentTallatacosuela.getTacosuelaFk();
            Tacosuela tacosuelaFkNew = tallatacosuela.getTacosuelaFk();
            if (inventarioNew != null) {
                inventarioNew = em.getReference(inventarioNew.getClass(), inventarioNew.getCodinventario());
                tallatacosuela.setInventario(inventarioNew);
            }
            if (tallaFkNew != null) {
                tallaFkNew = em.getReference(tallaFkNew.getClass(), tallaFkNew.getCodtalla());
                tallatacosuela.setTallaFk(tallaFkNew);
            }
            if (tacosuelaFkNew != null) {
                tacosuelaFkNew = em.getReference(tacosuelaFkNew.getClass(), tacosuelaFkNew.getCodmateriaprima());
                tallatacosuela.setTacosuelaFk(tacosuelaFkNew);
            }
            tallatacosuela = em.merge(tallatacosuela);
            if (inventarioOld != null && !inventarioOld.equals(inventarioNew)) {
                inventarioOld.setTallatacosuelaFk(null);
                inventarioOld = em.merge(inventarioOld);
            }
            if (inventarioNew != null && !inventarioNew.equals(inventarioOld)) {
                Tallatacosuela oldTallatacosuelaFkOfInventario = inventarioNew.getTallatacosuelaFk();
                if (oldTallatacosuelaFkOfInventario != null) {
                    oldTallatacosuelaFkOfInventario.setInventario(null);
                    oldTallatacosuelaFkOfInventario = em.merge(oldTallatacosuelaFkOfInventario);
                }
                inventarioNew.setTallatacosuelaFk(tallatacosuela);
                inventarioNew = em.merge(inventarioNew);
            }
            if (tallaFkOld != null && !tallaFkOld.equals(tallaFkNew)) {
                tallaFkOld.getTallatacosuelaList().remove(tallatacosuela);
                tallaFkOld = em.merge(tallaFkOld);
            }
            if (tallaFkNew != null && !tallaFkNew.equals(tallaFkOld)) {
                tallaFkNew.getTallatacosuelaList().add(tallatacosuela);
                tallaFkNew = em.merge(tallaFkNew);
            }
            if (tacosuelaFkOld != null && !tacosuelaFkOld.equals(tacosuelaFkNew)) {
                tacosuelaFkOld.getTallatacosuelaList().remove(tallatacosuela);
                tacosuelaFkOld = em.merge(tacosuelaFkOld);
            }
            if (tacosuelaFkNew != null && !tacosuelaFkNew.equals(tacosuelaFkOld)) {
                tacosuelaFkNew.getTallatacosuelaList().add(tallatacosuela);
                tacosuelaFkNew = em.merge(tacosuelaFkNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = tallatacosuela.getCodtallatacosuela();
                if (findTallatacosuela(id) == null) {
                    throw new NonexistentEntityException("The tallatacosuela with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            
            em = getEntityManager();
            em.getTransaction().begin();
            Tallatacosuela tallatacosuela;
            try {
                tallatacosuela = em.getReference(Tallatacosuela.class, id);
                tallatacosuela.getCodtallatacosuela();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tallatacosuela with id " + id + " no longer exists.", enfe);
            }
            Inventario inventario = tallatacosuela.getInventario();
            if (inventario != null) {
                inventario.setTallatacosuelaFk(null);
                inventario = em.merge(inventario);
            }
            Talla tallaFk = tallatacosuela.getTallaFk();
            if (tallaFk != null) {
                tallaFk.getTallatacosuelaList().remove(tallatacosuela);
                tallaFk = em.merge(tallaFk);
            }
            Tacosuela tacosuelaFk = tallatacosuela.getTacosuelaFk();
            if (tacosuelaFk != null) {
                tacosuelaFk.getTallatacosuelaList().remove(tallatacosuela);
                tacosuelaFk = em.merge(tacosuelaFk);
            }
            em.remove(tallatacosuela);
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tallatacosuela> findTallatacosuelaEntities() {
        return findTallatacosuelaEntities(true, -1, -1);
    }

    public List<Tallatacosuela> findTallatacosuelaEntities(int maxResults, int firstResult) {
        return findTallatacosuelaEntities(false, maxResults, firstResult);
    }

    private List<Tallatacosuela> findTallatacosuelaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tallatacosuela.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tallatacosuela findTallatacosuela(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tallatacosuela.class, id);
        } finally {
            em.close();
        }
    }

    public int getTallatacosuelaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tallatacosuela> rt = cq.from(Tallatacosuela.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
     public List<Tallatacosuela> findTallatacosuelaWhereOrderBy(String consulta,boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
//            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
//            cq.select(cq.from(Cliente.class));
//            Query q = em.createQuery(cq);
//            Query q = em.createQuery("SELECT o FROM Cliente o where "+ +" order by o.numerocliente");
            Query q = em.createQuery(consulta);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
